#include "MakeHexMesh.hpp"

void make_hex_mesh(Eigen::MatrixXd &V, Eigen::MatrixXi &H, int x_grid,
                   int y_grid, int z_grid, const Eigen::Vector3d &start,
                   const Eigen::Vector3d &end) {
  V.resize((x_grid + 1) * (y_grid + 1) * (z_grid + 1), 3);
  H.resize(x_grid * y_grid * z_grid, 8);
  int index = 0;
  for (int i = 0; i <= z_grid; i++) {
    for (int j = 0; j <= y_grid; j++) {
      for (int k = 0; k <= x_grid; k++) {
        // std::cout<<"i,j,k: " << i << " " << j << " " << k << " " <<
        // std::endl;
        V.row(index) = Eigen::Vector3d{
            start(0) + (end(0) - start(0)) * k / x_grid,
            start(1) + (end(1) - start(1)) * j / y_grid,
            start(2) + (end(2) - start(2)) * i / z_grid};
        index++;
      }
    }
  }
  index = 0;
  for (int i = 0; i < z_grid; i++) {
    for (int j = 0; j < y_grid; j++) {
      for (int k = 0; k < x_grid; k++) {
        int tmp = k + (x_grid + 1) * j + (x_grid + 1) * (y_grid + 1) * i;
        H(index, 0) = tmp;
        H(index, 1) = tmp + 1;
        H(index, 2) = tmp + 1 + x_grid + 1;
        H(index, 3) = tmp + x_grid + 1;
        tmp = tmp + (x_grid + 1) * (y_grid + 1);
        H(index, 4) = tmp;
        H(index, 5) = tmp + 1;
        H(index, 6) = tmp + 1 + x_grid + 1;
        H(index, 7) = tmp + x_grid + 1;
        index++;
      }
    }
  }
}